package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"
	"github.com/kataras/iris/v12"
)

type Rank struct {
	Ctx iris.Context
}

// 获取位次列表
func (c *Rank) PostList() interface{} {
	year := c.Ctx.PostValue("year")
	subject := c.Ctx.PostValue("subject")

	if len(year) <= 0 {
		return utils.CommonRes(1000, "请选择年份", "")
	}
	if len(subject) <= 0 {
		return utils.CommonRes(1000, "请选择科目", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	rankList := make([]models.Rank, 0)
	err = db.Where("year = ? and subject = ?", year, subject).Find(&rankList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取位次列表失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取位次列表", rankList)
}
